package org.aplikator.client.shared.data;

import org.jboss.errai.common.client.api.annotations.Portable;

import java.io.Serializable;

@SuppressWarnings("serial")
@Portable
public class PrimaryKey implements Serializable {
	public static final String serializationToken = "$";
    private int id;
    private String tempId;
    private String entityId;
    
    public PrimaryKey() {//TODO custom marshaller
    }

	public String getSerializationString() {
	    String pkString = PrimaryKey.serializationToken+ this.getEntityId() + PrimaryKey.serializationToken + this.getId() + PrimaryKey.serializationToken;
	    if (tempId==null) {
		return pkString; 
	    } else {
	        return pkString+this.tempId+PrimaryKey.serializationToken;
	    }
	}
	
    public PrimaryKey(String entityId, int id) {
        this.entityId = entityId;
        this.id = id;
    }
    
    public PrimaryKey(String serializedValue) {
    	String[] serializedArray = serializedValue.split("\\"+PrimaryKey.serializationToken);
    	if (serializedArray.length==3) {
            this.entityId=serializedArray[1];
            this.id=Integer.valueOf(serializedArray[2]);
    	} else if (serializedArray.length==4) {
    	    this.entityId=serializedArray[1];
    	    this.id=Integer.valueOf(serializedArray[2]);
    	    this.tempId=serializedArray[3];
    	} else {
    		throw new IllegalStateException("Wrong primary key:"+serializedValue);
        }
    }
    

    public PrimaryKey(String entityId, String tempId) {
        this.entityId = entityId;
        this.tempId = tempId;
        this.id = -1;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTempId() {
        return tempId;
    }

    public void setTempId(String tempId) {
        this.tempId = tempId;
    }

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
		this.entityId = entityId;
	}
    
    @Override
    public String toString() {
        return "PrimaryKey [entity=" + entityId + ", id=" + id + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((entityId == null) ? 0 : entityId.hashCode());
        result = prime * result + id;
        result = prime * result + ((tempId == null) ? 0 : tempId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof PrimaryKey))
            return false;
        PrimaryKey other = (PrimaryKey) obj;
        if (entityId == null) {
            if (other.entityId != null)
                return false;
        } else if (!entityId.equals(other.entityId))
            return false;
        if (id!=-1 && other.id!= -1 && id != other.id)
            return false;
        if (tempId == null) {
            if (other.tempId != null)
                return false;
        } else if (!tempId.equals(other.tempId))
            return false;
        return true;
    }

}
